<template>
  <view class="shop_footer fixIphonex" v-if="showFooter">
    <view class="item" v-for="item in tabList">
      <button :key="item.url" hover-class="none" @click="changePage($event, item)">
        <block v-if="isLogin && item.showNum">
          <view v-if="carNumber != 0 && carNumber < 100" class="red-label">{{ carNumber }}</view>
          <view v-if="carNumber > 99" class="red-label">...</view>
        </block>
        <image v-show="currentUrl == item.url" :src="item.checkedIcon"></image>
        <image v-show="currentUrl != item.url" :src="item.unCheckedIcon"></image>
        <view :class="{ red: currentUrl == item.url }">{{ item.iconName }}</view>
      </button>
    </view>
    <public-dialog ref="dialog"></public-dialog>
  </view>
</template>

<script>
import icon_index from '@/static/public/home.png'
import icon_index_sel from '@/static/public/homeFill.png'
import icon_stand from '@/static/public/new.png'
import icon_stand_sel from '@/static/public/newFill.png'
import icon_user from '@/static/public/my.png'
import icon_user_sel from '@/static/public/myFill.png'
import config from '@/config'
export default {
  props: {
    currentUrl: {
      type: String,
    },
    pageType: {
      type: Number,
      default: 1, //云精选，云课程
    },
  },
  computed: {
    showFooter() {
      const haveCurrentUrl = this.tabList.findIndex((item) => item.url == this.currentUrl)
      return haveCurrentUrl != -1
    },
    carNumber() {
      return this.$globalData.state.shoppingCarNum || 0
    },
    isLogin: function () {
      return this.$globalData.state.token
    },
    tabList() {
      const tab = [
        {
          unCheckedIcon: icon_index,
          checkedIcon: icon_index_sel,
          iconName: '首页',
          login: false,
          url: '/pages/index/index',
        },
        {
          unCheckedIcon: icon_stand,
          checkedIcon: icon_stand_sel,
          iconName: '站内信',
          login: true,
          url: '/pages/new/mail',
        },
        {
          unCheckedIcon: icon_user,
          checkedIcon: icon_user_sel,
          iconName: '个人中心',
          login: false,
          url: '/pages/user/index',
        },
      ]
      return tab
    },
  },
  methods: {
    changePage($event, item) {
      console.log('什么东西', item)
      if (item.url == this.currentUrl) return
      let pageArr = ['/pages/index/index', '/pages/user/index', '/pages/new/mail']
      let navFn = pageArr.includes(item.url)
        ? this.$routeUtils.switchTab
        : this.$routeUtils.navigateTo
      if (item.login && !this.$globalData.state.token) {
        this.$refs.dialog.confirm({
          title: '该功能需登录，是否跳转登录页？',
          confirmBtnFn: () => {
            const realNavUrl = encodeURIComponent(item.url)
            this.$routeUtils.navigateTo({
              url: `/pages/user/login?realNavUrl=${realNavUrl}`,
            })
          },
        })
      } else {
        navFn({ url: item.url })
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.shop_footer {
  background: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  padding: 10rpx 0;
  height: 80rpx;
  z-index: 51;
  width: 100%;
  box-shadow: 0px 0px 0px 1px rgba(221, 221, 221, 0.5);
  display: flex;
  .item {
    text-align: center;
    flex: 1;
    font-size: 0;
    color: #333;
    position: relative;
    image {
      width: 52rpx;
      height: 52rpx;
    }
    view {
      font-size: 20rpx;
      &.red {
        color: #3c9cff;
      }
    }
  }
  .red-label {
    font-size: 8px !important;
    background: #ff004f;
    border-radius: 50%;
    width: 12px;
    height: 12px;
    line-height: 12px;
    position: absolute;
    left: 52%;
    color: white;
    z-index: 1;
  }
}
</style>
